import os, sys, time
import argparse

parser = argparse.ArgumentParser(description="")
parser.add_argument('-n','--name', default='test')
args = parser.parse_args()

if args.name == "test":
    cmd0 = "mkd data && mkd image && mkd files && mkd templates"
    os.system(cmd0)
    
    cmd = "ps -aux | grep flask_main | grep -v color"

    res = os.popen(cmd).read()

    res2 = res.split("\n")
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), len(res2), res2)
    for i in range(len(res2)):
        try:
            tmp = res2[i].split()
            print(i, tmp, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
            cmd_kill = "kill -9 %s"%(tmp[1])
            runres = os.system(cmd_kill)
        except:
            continue

    time.sleep(5)

    restart_cmd = "~/pythoncode/env1/bin/python flask_main.py >>./testlog2 2>&1 &"

    restart_res = os.system(restart_cmd)

    print(restart_res)
elif args.name == "fresh":
    cmd0 = "rm -r ./data/* && rm ./files/* && rm ./image/* && rm ./templates/* && rm ./id2file.txt && rm ./file2id.txt"
    os.system(cmd0)
